package com.ruoyi.lxzx.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.lxzx.appVO.lxlbVO.LxlbListVO;
import com.ruoyi.lxzx.domain.UserAttentList;
import com.ruoyi.lxzx.domain.UserAttentSearch;
import com.ruoyi.lxzx.domain.UserCard;
import com.ruoyi.lxzx.service.IUserCardService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.lxzx.mapper.UserAttentMapper;
import com.ruoyi.lxzx.domain.UserAttent;
import com.ruoyi.lxzx.service.IUserAttentService;

/**
 * 关住Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-26
 */
@Service
@AllArgsConstructor
public class UserAttentServiceImpl implements IUserAttentService {
    private final UserAttentMapper userAttentMapper;

    /**
     * 查询关住
     *
     * @param id 关住主键
     * @return 关住
     */
    @Override
    public UserAttent selectUserAttentById(Long id) {
        return userAttentMapper.selectUserAttentById(id);
    }

    /**
     * 查询关住列表
     *
     * @param search@return 关住
     */
    @Override
    public List<UserAttentList> selectUserAttentList(UserAttentSearch search) {
        if ("1".equals(search.getType())) {
            return userAttentMapper.selectUserAttentList1(search);
        } else if ("2".equals(search.getType())) {
            return userAttentMapper.selectUserAttentList2(search);
        } else {
            return new ArrayList<UserAttentList>();
        }
    }

    /**
     * 新增关住
     *
     * @param userAttent 关住
     * @return 结果
     */
    @Override
    public int insertUserAttent(UserAttent userAttent) {
        userAttent.setCreateTime(DateUtils.getNowDate());
        return userAttentMapper.insertUserAttent(userAttent);
    }

    /**
     * 修改关住
     *
     * @param userAttent 关住
     * @return 结果
     */
    @Override
    public int updateUserAttent(UserAttent userAttent) {
        userAttent.setUpdateTime(DateUtils.getNowDate());
        return userAttentMapper.updateUserAttent(userAttent);
    }

    /**
     * 批量删除关住
     *
     * @param ids 需要删除的关住主键
     * @return 结果
     */
    @Override
    public int deleteUserAttentByIds(Long[] ids) {
        return userAttentMapper.deleteUserAttentByIds(ids);
    }

    /**
     * 删除关住信息
     *
     * @param id 关住主键
     * @return 结果
     */
    @Override
    public int deleteUserAttentById(Long id) {
        return userAttentMapper.deleteUserAttentById(id);
    }

    @Override
    public void gzlx(Long userId) {
        Long finalUserId = 0L;
        String finalUserName = "admin";
        try {
            finalUserId = SecurityUtils.getUserId();
            finalUserName = SecurityUtils.getUsername();
        } catch (Exception e) {
                throw new ServiceException("当前未登录,或登录已过期");
        }
        if (userId.equals(finalUserId)) {
            throw new ServiceException("自己不能进行关注");
        }
        UserAttent old = userAttentMapper.findGuanZhuByUserId(userId, finalUserId);
        if (old != null) {
            old.setAttentType("2");
            this.updateUserAttent(old);
        } else {
            UserAttent entity = new UserAttent();
            entity.setUserId(finalUserId);
            entity.setAttentUserId(userId);
            entity.setAttentType("1");
            entity.setCreateBy(finalUserName);
            entity.setCreateTime(new Date());
            this.insertUserAttent(entity);
        }
    }

    @Override
    public String gzStatus(Long userId, Long userId1) {
        return userAttentMapper.gzStatus(userId, userId1);
    }

    @Override
    public void qxgzlx(Long userId) {
        Long finalUserId = 0L;
        try {
            finalUserId = SecurityUtils.getUserId();
        } catch (Exception e) {
                throw new ServiceException("当前未登录,或登录已过期");
        }
        if (userId.equals(finalUserId)) {
            throw new ServiceException("自己不能进行关注");
        }
        UserAttent old = userAttentMapper.findGuanZhuByUserId(userId, finalUserId);
        if (old != null && old.getAttentType().equals("1")) {
            userAttentMapper.deleteUserAttentById(old.getId());
        } else if (old != null && old.getAttentType().equals("2")) {
            old.setAttentType("1");
            userAttentMapper.updateUserAttent(old);
        }
    }
}
